﻿using ShareFrame.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using NSwag.Annotations;
using System;
using System.IO;
using Microsoft.AspNetCore.Authorization;
using ShareFrame.Entity.Base_Manage;
using ShareFrame.IBusiness.Base_Manage;
using ShareFrame.Entity.DTO;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ShareFrame.Api.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    [OpenApiTag("上传操作测试接口", Description = "用于上传操作测试")]
    [ApiExplorerSettings(GroupName = "主页登录及测试")]
    public class UploadController : BaseApiController
    {
        
        public UploadController
            (
            IConfiguration configuration,
            IBase_UploadTypeBusiness base_UploadTypeBus,
            IBase_UploadRecordsBusiness base_UploadRecordsBus,
            IOptions<BaseDbOption> basedbOptions
            )
        {
            _configuration = configuration;
            _basedbOptions = basedbOptions.Value;
            _base_UploadTypeBus = base_UploadTypeBus;
            _base_UploadRecordsBus = base_UploadRecordsBus;
        }
        private readonly IConfiguration _configuration;
        private readonly BaseDbOption _basedbOptions;
        IBase_UserBusiness _userBus { get; }
        IBase_UploadTypeBusiness _base_UploadTypeBus { get; }
        IBase_UploadRecordsBusiness _base_UploadRecordsBus { get; }

        /// <summary>
        /// 文件上传测试接口
        /// </summary>
        /// <param name="file">文件对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadFileByForm(IFormFile file)
        {
            Base_UploadType uploadType = await _base_UploadTypeBus.GetTheDataAsync(_basedbOptions, "Base_Test");
            IActionResult result = UploadFileBase(file, _configuration, uploadType.FolderName);
            JObject jo = (JObject)JsonConvert.DeserializeObject(result.ToJson());
            jo = (JObject)JsonConvert.DeserializeObject(jo["Content"].ToString());
            if (jo["status"].ToString().Equals("done"))
            {
                Base_UploadRecords bur = new Base_UploadRecords();
                bur.TypeName = "测试文件";
                bur.FileURL = jo["url"].ToString();
                bur.Remark = "上传测试文件";
                InitEntity(bur);
                await _base_UploadRecordsBus.AddDataAsync(bur);
            }
            return result;
        }
    }
}
